/*   1:    */ package com.atomikos.util;
/*   2:    */ 
/*   3:    */ import java.io.DataInputStream;
/*   4:    */ import java.io.DataOutputStream;
/*   5:    */ import java.io.FileDescriptor;
/*   6:    */ import java.io.FileInputStream;
/*   7:    */ import java.io.FileNotFoundException;
/*   8:    */ import java.io.FileOutputStream;
/*   9:    */ import java.io.IOException;
/*  10:    */ import java.util.Date;
/*  11:    */ 
/*  12:    */ public class UniqueIdMgr
/*  13:    */ {
/*  14: 46 */   private static long MAX_PER_EPOCH = 32000L;
/*  15:    */   private static final int MAX_LENGTH_OF_NUMERIC_SUFFIX = 16;
/*  16:    */   String server_;
/*  17:    */   long epoch_;
/*  18:    */   long lastcounter_;
/*  19:    */   int limit_;
/*  20:    */   private String prefix_;
/*  21:    */   private String suffix_;
/*  22:    */   private VersionedFile file_;
/*  23:    */   
/*  24:    */   public UniqueIdMgr(String server)
/*  25:    */   {
/*  26: 71 */     this(server, "./");
/*  27: 72 */     this.limit_ = -1;
/*  28: 73 */     this.server_ = server;
/*  29: 74 */     this.epoch_ = new Date().getDate();
/*  30: 75 */     this.lastcounter_ = 0L;
/*  31:    */   }
/*  32:    */   
/*  33:    */   public UniqueIdMgr(String server, String directorypath)
/*  34:    */   {
/*  35: 88 */     this(server, directorypath, -1);
/*  36:    */   }
/*  37:    */   
/*  38:    */   public UniqueIdMgr(String server, String directorypath, int limit)
/*  39:    */   {
/*  40:101 */     this.limit_ = limit;
/*  41:102 */     this.server_ = server;
/*  42:103 */     this.file_ = new VersionedFile(directorypath, server, ".epoch");
/*  43:104 */     setSuffix("");
/*  44:105 */     setPrefix("");
/*  45:    */     try
/*  46:    */     {
/*  47:109 */       this.epoch_ = readEpoch();
/*  48:    */     }
/*  49:    */     catch (Exception e)
/*  50:    */     {
/*  51:112 */       throw new RuntimeException(e.getMessage());
/*  52:    */     }
/*  53:114 */     this.lastcounter_ = 0L;
/*  54:    */   }
/*  55:    */   
/*  56:    */   public void setSuffix(String suffix)
/*  57:    */   {
/*  58:126 */     this.suffix_ = suffix;
/*  59:    */   }
/*  60:    */   
/*  61:    */   public void setPrefix(String prefix)
/*  62:    */   {
/*  63:138 */     this.prefix_ = prefix;
/*  64:    */   }
/*  65:    */   
/*  66:    */   protected long readEpoch()
/*  67:    */     throws IOException
/*  68:    */   {
/*  69:154 */     long ret = 0L;
/*  70:    */     try
/*  71:    */     {
/*  72:158 */       FileInputStream fin = this.file_.openLastValidVersionForReading();
/*  73:159 */       DataInputStream din = new DataInputStream(fin);
/*  74:160 */       ret = din.readLong();
/*  75:161 */       din.close();
/*  76:162 */       fin.close();
/*  77:    */     }
/*  78:    */     catch (FileNotFoundException firstStartup) {}
/*  79:168 */     writeEpoch(ret + 1L);
/*  80:170 */     if (ret + 1L <= 0L) {
/*  81:171 */       throw new RuntimeException("Epoch overflow!");
/*  82:    */     }
/*  83:173 */     return ret + 1L;
/*  84:    */   }
/*  85:    */   
/*  86:    */   protected void writeEpoch(long value)
/*  87:    */     throws IOException
/*  88:    */   {
/*  89:184 */     if ((value <= 0L) || ((this.limit_ >= 0) && (value > this.limit_))) {
/*  90:185 */       throw new RuntimeException("Epoch overflow!");
/*  91:    */     }
/*  92:188 */     FileOutputStream fout = this.file_.openNewVersionForWriting();
/*  93:189 */     DataOutputStream dout = new DataOutputStream(fout);
/*  94:190 */     dout.writeLong(value);
/*  95:191 */     dout.flush();
/*  96:192 */     fout.flush();
/*  97:    */     
/*  98:194 */     fout.getFD().sync();
/*  99:195 */     dout.close();
/* 100:196 */     fout.close();
/* 101:197 */     this.file_.discardBackupVersion();
/* 102:198 */     this.file_.close();
/* 103:    */   }
/* 104:    */   
/* 105:    */   private String getCountWithLeadingZeroes(long number)
/* 106:    */   {
/* 107:204 */     String ret = Long.toString(number);
/* 108:205 */     int max = Long.toString(MAX_PER_EPOCH).length();
/* 109:206 */     int len = ret.length();
/* 110:207 */     StringBuffer zeroes = new StringBuffer();
/* 111:208 */     while (len < max)
/* 112:    */     {
/* 113:209 */       zeroes.append("0");
/* 114:210 */       len++;
/* 115:    */     }
/* 116:212 */     ret = ret;
/* 117:213 */     return ret;
/* 118:    */   }
/* 119:    */   
/* 120:    */   public synchronized String get()
/* 121:    */   {
/* 122:224 */     this.lastcounter_ += 1L;
/* 123:225 */     if (this.lastcounter_ > MAX_PER_EPOCH)
/* 124:    */     {
/* 125:226 */       this.lastcounter_ = 0L;
/* 126:227 */       this.epoch_ += 1L;
/* 127:    */       try
/* 128:    */       {
/* 129:229 */         writeEpoch(this.epoch_);
/* 130:    */       }
/* 131:    */       catch (Exception e)
/* 132:    */       {
/* 133:232 */         throw new RuntimeException(e.getMessage());
/* 134:    */       }
/* 135:    */     }
/* 136:236 */     return getCommonPartOfId() + getCountWithLeadingZeroes(this.lastcounter_) + getCountWithLeadingZeroes(this.epoch_);
/* 137:    */   }
/* 138:    */   
/* 139:    */   private String getCommonPartOfId()
/* 140:    */   {
/* 141:240 */     StringBuffer ret = new StringBuffer(64);
/* 142:241 */     ret.append(this.prefix_).append(this.server_).append(this.suffix_);
/* 143:242 */     return ret.toString();
/* 144:    */   }
/* 145:    */   
/* 146:    */   public int getMaxIdLengthInBytes()
/* 147:    */   {
/* 148:247 */     return getCommonPartOfId().getBytes().length + 16;
/* 149:    */   }
/* 150:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.util.UniqueIdMgr
 * JD-Core Version:    0.7.0.1
 */